package com.keyancompany.zhongbaopingtai.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.LoadTable;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.HistogramView;
import com.keyancompany.zhongbaopingtai.weight.LineChartView;

import java.util.Arrays;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.ButterKnife;

/**
 * 项目进度
 */
public class ProjectProgressActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @BindString(R.string.project_progerss)
    String title;
    @Bind(R.id.histogramView)
    HistogramView histogramView;
    @Bind(R.id.lineCartView)
    LineChartView lineCartView;
    @Bind(R.id.PinChart)
    com.keyancompany.zhongbaopingtai.weight.PinChart pinChart;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_project_progress;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, title);
    }

    @Override
    protected void setUpViews() {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
      if (isNetvaiable()) {
          Toast.makeText(mContext,"当前teamCode"+SPUtils.getCommData(mContext,Constants.TEAM_CODE,"T"),Toast.LENGTH_LONG).show();
          String teamCode = String.valueOf(SPUtils.getCommData(mContext,Constants.TEAM_CODE,"null"));
          TeamAPI.loadTable(teamCode,ProjectProgressActivity.this,getLoading(R.string.watting_hint));
            }
        }
    @Override
    public void success(int type, String json) {
        super.success(type, json);
           if (validateJson(json) != null) {
            BaseModel base = JSONObject.parseObject(json, BaseModel.class);
            if (base.result.equals(Constants.SUCCESS)){
                LoadTable loadTable = JSONObject.parseObject(base.data, LoadTable.class);
                    if(loadTable.getListrows().size()>0){
                        int[] arr = new int[loadTable.getListrows().size()];
                        for(int i=0;i<loadTable.getListrows().size();i++){//获取状态字符串数组
                            arr[i] = loadTable.getListrows().get(i).getCc();
                        }
/////////////////////////柱状图
                        int histogramCount=0;
                        for(int i=0;i<arr.length;i++){//获取字符串数组总和
                            histogramCount=histogramCount+arr[i];
                        }
                        int[] histogramArr= Arrays.copyOf(arr,arr.length+1);//加 总和的数组，用来传给柱状图
                        histogramArr[arr.length]=histogramCount;

                        histogramView.setData(histogramArr);
                        histogramView.start(2);
/////////////////////////扇形图
                        float[] pinArr =new float[arr.length];
                        for(int i=0;i<arr.length;i++){
                            pinArr[i] = arr[i]*360/histogramCount;
                        }
                        pinChart.setData(pinArr);
                        pinChart.start(2);
                    }else{
                        T.showShort(mContext,"当前暂无项目信息");
                    }
            } else {
                T.showShort(mContext, base.msg);
            }
        }
    }
    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext,error);
    }
 }